package fr.neyb.bernouilli.problem014;

public class Entry {

	private final static int limit = 1_000_000;

	public static void main(String... args) throws Exception {
		int maxRoot = 1;
		int maxLength = 1;
		int currL;

		for (int currRoot = 1; currRoot <= limit; ++currRoot) {
			currL = ChainLengthOf(currRoot);
			if (maxLength < currL) {
				maxLength = currL;
				maxRoot = currRoot;
			}
		}

		System.out.println(maxRoot);
	}

	private static int ChainLengthOf(int rootInt) {
		long curr = rootInt;
		int ChainLength = 1;

		while (curr != 1) {
			++ChainLength;
			if (curr % 2 == 0) {
				curr /= 2;
			} else {
				curr = 3 * curr + 1;
			}
		}

		return ChainLength;
	}
}
